package List;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Objects;

/**
 * @author yangwenqi
 * @decs TODO
 * @date 2025/4/28
 * 2.去除ArrayList集合中重复元素
 *   1>.存入字符串元素
 *   2>.存入自定义对象元素（如Person对象）
 */
public class ArrayLists {
    public static void main(String[] args) {
        ArrayList<Person> list = new ArrayList<>();
        Person p1 = new Person("张三",19, '男');
        Person p2 = new Person("钱七",28, '女');
        Person p3 = new Person("李四",29, '男');
        Person p4 = new Person("王五",17, '女');
        Person p5 = new Person("赵六",36, '男');
        Person p6 = new Person("李四",29, '男');
        list.add(p1);
        list.add(p2);
        list.add(p3);
        list.add(p4);
        list.add(p5);
        list.add(p6);
        System.out.println(list);
        for(int i = 0; i<list.size(); i++){
            for(int j = i+1; j<list.size(); j++){
                if(list.get(i).equals(list.get(j))){
                    list.remove(j);
                }
            }
        }
        System.out.println(list);

    }
}
class Person{
    private String name;
    private int age;
    private char gender;
    public Person() {
    }

    public Person(String name, int age, char gender) {
        this.name = name;
        this.age = age;
        this.gender = gender;
    }

    /**
     * 获取
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取
     * @return age
     */
    public int getAge() {
        return age;
    }

    /**
     * 设置
     * @param age
     */
    public void setAge(int age) {
        this.age = age;
    }

    /**
     * 获取
     * @return gender
     */
    public char getGender() {
        return gender;
    }

    /**
     * 设置
     * @param gender
     */
    public void setGender(char gender) {
        this.gender = gender;
    }

    public String toString() {
        return "Person{name = " + name + ", age = " + age + ", gender = " + gender + "}";
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Person person = (Person) o;
        return age == person.age && gender == person.gender && Objects.equals(name, person.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age, gender);
    }
}
